package com.unicom.projt.fee;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.util.*;

import java.text.DecimalFormat;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-1-4
 * Time: 下午8:57
 */
public class IvrPasswordInputPage extends Activity implements View.OnClickListener {
    private EditText mCardPwd;
    private TextView mPhoneNumber;
    private TextView mAmount;
    private Double amount;
    private String phoneNumber;
    private String bankName;
    private String cardNumber;
    private TextView mBankTxt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ivr_password_input);
        Bundle extras = getIntent().getExtras();
        amount = extras.getDouble("amount");
        phoneNumber = extras.getString("phoneNumber");
        bankName = extras.getString("bankName");
        cardNumber = extras.getString("cardNumber");
        mCardPwd = (EditText) this.findViewById(R.id.ivr_pwd_input_card_pwd);
        mPhoneNumber = (TextView) this.findViewById(R.id.ivr_pwd_input_phone_number);
        mAmount = (TextView) this.findViewById(R.id.ivr_pwd_input_amount);
        mPhoneNumber.setText(phoneNumber);
        mAmount.setText(new DecimalFormat("###,###,###.##").format(amount) + "");
        mBankTxt = (TextView) this.findViewById(R.id.ivr_pwd_input_bank_txt);
        mBankTxt.setText(Html.fromHtml("请输入尾号为<font color='red'>"+cardNumber+"</font>"+bankName+"的支付密码") );

        this.findViewById(R.id.ivr_pwd_input_next_btn).setOnClickListener(this);
        this.findViewById(R.id.ivr_pwd_input_back_btn).setOnClickListener(this);
        this.findViewById(R.id.ivr_pwd_input_logout_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivr_pwd_input_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.ivr_pwd_input_logout_btn:
//                PageJumpUtils.logoutAndJumpToMainPage(this);
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
            case R.id.ivr_pwd_input_next_btn:
                startPaymentConfirmPage();
                break;
        }
    }
    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }
    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void startPaymentConfirmPage() {
        String password = mCardPwd.getText().toString().trim();
        if ("".equals(password)) {
            ToastUtils.showMessage(this, "请输入支付密码");
            return;
        }else if(!checkCardPassword(password)){
            ToastUtils.showMessage(this, "支付密码必须是6位数字");
            return;
        }
        else {
            Intent intent = new Intent(this, PaymentConfirmPage.class);
            intent.putExtra("amount", amount);
            intent.putExtra("phoneNumber", phoneNumber);
            intent.putExtra("password", password);
            intent.putExtra("isIvrPay", true);
            this.startActivity(intent);
            this.finish();
        }
    }

    private boolean checkCardPassword(String password) {
        if(password.length()<6){
            return false;
        }else if(!CheckUtils.isNumber(password)){
            return false;
        }
        return true;
    }
}
